package com.yinp.proapp.module.game.tanchishe;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.Nullable;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author :yinpeng
 * date      :2021/9/7
 * package   :com.yinp.proapp.module.game.tanchishe
 * describe  :
 */
public class GluttonousSnakeSurfaceView extends SurfaceView {
    private String TAG = "abcd";

    public GluttonousSnakeSurfaceView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GluttonousSnakeSurfaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private Paint snakePaint;
    private Paint snakePaint2;
    private int snakeWidth;
    private Paint paint;
    private ALinkList aLinkList;

    private void init() {
        snakePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        snakePaint2 = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        path = new Path();
        paint = new Paint();
        paint.setColor(Color.RED);
        aLinkList = new ALinkList();
//        setLayerType(LAYER_TYPE_HARDWARE, null);
//        setWillNotDraw(false);
    }

    Path path;
    int width;
    int height;
    boolean isFirst = true;
    SurfaceHolder sfh;

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d(TAG, "onLayout: ");
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d(TAG, "onMeasure: ");
        if (isFirst) {
            width = MeasureSpec.getSize(widthMeasureSpec);
            height = MeasureSpec.getSize(heightMeasureSpec);
            snakeWidth = width / 100;
            snakePaint.setStrokeWidth(snakeWidth);
            snakePaint.setColor(Color.RED);
            snakePaint2.setStrokeWidth(snakeWidth);
            snakePaint2.setColor(Color.TRANSPARENT);

            aLinkList.insertFirst(width / 2, width / 2);
            aLinkList.insertFirst(width / 2 - snakeWidth, width / 2);
            aLinkList.insertFirst(width / 2 - snakeWidth * 2, width / 2);
            aLinkList.insertFirst(width / 2 - snakeWidth * 3, width / 2);
            aLinkList.insertFirst(width / 2 - snakeWidth * 4, width / 2);
            aLinkList.insertFirst(width / 2 - snakeWidth * 5, width / 2);

            isFirst = false;
            sfh = getHolder();
            sfh.addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
//                    MOnDraw();
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

                }

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                }
            });

        }
    }

    private int direction = 2;

    public void setDirection(int gravity) {
        if (direction + gravity == 2 || direction + gravity == 4) {
            return;
        }
        this.direction = gravity;
    }

    private void start() {
        switch (direction) {
            case 0://左
                aLinkList.insertFirst(aLinkList.first.x - snakeWidth, aLinkList.first.y);
                break;
            case 1://上
                aLinkList.insertFirst(aLinkList.first.x, aLinkList.first.y - snakeWidth);
                break;
            case 2://右
                aLinkList.insertFirst(aLinkList.first.x + snakeWidth, aLinkList.first.y);
                break;
            case 3://下
                aLinkList.insertFirst(aLinkList.first.x, aLinkList.first.y + snakeWidth);
                break;
        }
        aLinkList.removeLast();
        MOnDraw();
    }

    private Timer timer = new Timer();
    private TimerTask timerTask;

    public void startMove() {
        if (timerTask == null) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    start();
                }
            };
        }
        timer.schedule(timerTask, 0, 100);
    }

    public void stopMove() {
        timer.cancel();
    }

    public void remove() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
        }
        return super.onTouchEvent(event);
    }

    MyThread myThread = new MyThread();

    class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            Canvas canvas = sfh.lockCanvas();
//            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            canvas.drawColor(Color.WHITE);
            canvas.drawPoints(aLinkList.getAll(), snakePaint);
//            canvas.drawCircle(200, 200, 200, paint);
            sfh.unlockCanvasAndPost(canvas);
        }
    }

    private Bitmap bitmap;
    Canvas mcanvas;
    int count = 0;

    protected void MOnDraw() {
        count++;
        new Thread(() -> {
                Canvas canvas = sfh.lockCanvas();
//            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
//            canvas.drawPoints(aLinkList.getAll(), snakePaint);
                canvas.drawColor(Color.WHITE);

                canvas.drawPoint(aLinkList.first.x, aLinkList.first.y, snakePaint);
                canvas.drawPoint(aLinkList.first.next.x, aLinkList.first.next.y, snakePaint);
//            canvas.drawColor(Color.WHITE);
//            canvas.drawBitmap(bitmap, 0, 0, new Paint());
//            canvas.drawCircle(200, 200, 200, paint);
                sfh.unlockCanvasAndPost(canvas);
        }).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG, "onDraw: ");
        canvas.drawPoint(aLinkList.first.x, aLinkList.first.y, snakePaint);
        canvas.drawPoint(aLinkList.first.next.x, aLinkList.first.next.y, snakePaint);
    }

    private void clear() {
        Canvas canvas = sfh.lockCanvas(null);
        canvas.drawColor(Color.WHITE);
        sfh.unlockCanvasAndPost(canvas);
    }
}
